package com.example.bmipractice;

import static androidx.constraintlayout.helper.widget.MotionEffect.TAG;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.math.BigDecimal;
public class RateActivity extends AppCompatActivity {
    public final float wonRate = 510.00f;
    public final float dollarRate = 0.12f;
    public final float euroRate = 0.08f;
    EditText rmb;
    TextView out;

    // private float eurp_date;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rate);
        rmb = findViewById(R.id.rmb);
        out = findViewById(R.id.rmb_show);
    }

    public void click(View btn) {
        String inpStr = rmb.getText().toString();
        try {
            float inp = Float.parseFloat(inpStr);
            float result = 0f;
            //计算
            if (btn.getId() == R.id.btn_dollar) {
                result = inp * dollarRate;
            } else if (btn.getId() == R.id.btn_euro) {
                result = inp* euroRate;
            } else {
                result = inp * wonRate;
            }
            out.setText(String.valueOf(result));
        } catch (NumberFormatException ex) {
            //out.setText(R.string.no_inp_tip);
            Toast.makeText(this, "请输入金额后再计算", Toast.LENGTH_LONG).show();
        }
    }
    public void click2(View btn){
        Log.i(TAG,"click2:");
        Intent jdintent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.jd.com"));
        startActivity(jdintent);
    }
    public void config(View v){
        Intent configActivity = new Intent(this,ConfigActivity.class);
        configActivity.putExtra("dollar_rate_key",dollarRate);
        configActivity.putExtra("euro_rate_key",euroRate);
        configActivity.putExtra("won_rate_key",wonRate);

        Log.i(TAG,"config:dollarRate:"+dollarRate);
        Log.i(TAG,"config:euroRate:"+euroRate);
        Log.i(TAG,"config:wonRate:"+wonRate);
        //startActivity(intent);
        startActivityForResult(configActivity,3);
    }
    public void onActivityResult(int requestCode,int resultCode,@Nullable Intent data){
        if(requestCode==3&&requestCode==6){
            //汇率修改返回页面的数据，说明data中包含Bundle对象
            //Intent data = getIntent();
            Bundle bdl=data.getExtras();//拆分放入的数据荐
            float dollar_Rate=bdl.getFloat("dollar_rate_key");
            float euro_Rate=bdl.getFloat("euro_rate_key");
            float won_Rate=bdl.getFloat("won_rate_key");
            Log.i(TAG,"onActivityResult:dollarRate="+dollar_Rate);
            Log.i(TAG,"onActivityResult:euroRate="+euro_Rate);
            Log.i(TAG,"onActivityResult:wonRate="+won_Rate);
        }
        super.onActivityResult(requestCode,resultCode,data);
    }
    public void score(View view){
        Intent scoreintent = new Intent(this,ScoreActivity.class);
        startActivity(scoreintent);
    }
}